package au.com.handsunited.web.controller.organisation;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import au.com.handsunited.service.OrganisationService;
import au.com.handsunited.web.command.organisation.InviteOrganisationCommand;
import au.com.handsunited.web.session.SessionSupport;

/**
 * @author arunda
 *
 */
public class InviteOrganisationController extends SimpleFormController {

	private OrganisationService organisationService;


	private static Logger logger = LoggerFactory.getLogger(InviteOrganisationController.class);

	/**
	 *
	 */
	public InviteOrganisationController() {
		setCommandClass(InviteOrganisationCommand.class);
	}



	/**
	 * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, org.springframework.validation.BindException)
	 */
	@Override
	protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response,
			Object command, BindException errors) throws Exception {

		InviteOrganisationCommand inviteOrganisationCommand = (InviteOrganisationCommand) command;

		String organisationName = inviteOrganisationCommand.getOrganisationName();
		String contactName 		= inviteOrganisationCommand.getContactPerson();
		String emailAddress 	= inviteOrganisationCommand.getEmailAddress();
		String message 			= inviteOrganisationCommand.getMessage();

		organisationService.inviteOrganisation(SessionSupport.getUser(request),
				organisationName, contactName, emailAddress, message);

		Map<String, String> model = new HashMap<String, String>();
		model.put("organisationName", organisationName);

		ModelAndView mav = new ModelAndView(getSuccessView(), model);

		return mav;
	}


	/**
	 * @param organisationService the organisationService to set
	 */
	public void setOrganisationService(OrganisationService organisationService) {
		this.organisationService = organisationService;
	}

}

